﻿
var Article;
if (!Article) {
    Article = {};
}
Article.Init = function () {
    var editor;
    var addDlg = $('#dlg_Edit').dialog({
        title: "【添加分组】",
        autoOpen: false,
        modal: true,
        width: 900,
        height:500,
        open: function (event, ui) {

            editor = KindEditor.create('textarea[name="ContentText"]', {
               allowFileManager:true,
               allowUpload:true,
               fileManagerJson: 'Article/ProcessRequest',
               uploadJson: 'Article/UploadImage'
               //http://blog.163.com/very_apple/blog/static/277592362012111155310526/

            });
        },
        beforeClose: function (event, ui) {
            // 关闭Dialog前移除编辑器
            editor.remove('textarea[name="content"]');
        }
        ,
        buttons: {
            "关闭": function () {
                $(this).dialog("close");
            },
            "保存": function () {
                $("#ContentText").val(editor.html());
                $('#articleForm', addDlg).submit();
            }
        }
    });
    $('#articleForm').ajaxForm({
        dataType: 'json',
        success: function (data) {
            if (data.success) {
                alert("提交成功");
                addDlg.dialog('close');
                // window.location.reload(true);
            } else {
                alert(data.data);
            }

        }
    });

    //新增
    $("#create").click(function () {
        addDlg.resetInput();
       
        addDlg.dialog('open');
        return false;
    });
    //编辑
    $(".edit").live('click', function () {
       
        var href = $(this).attr("href");
        $('#dlg_Edit').bindDlgData(href, function (data) { editor.html(data.data.ContentText); });
        return false;
    });

    $('#HideOrShow').live('click', function() {
        var temp = $("#Atitle").is(':hidden');
        if (temp) {
            $("#Atitle").show();
            return;
        }
        $("#Atitle").hide();
    });

    //删除
    $(".dele").live('click', function () {
        var href = $(this).attr("href");
        $.ActionResponse('information', '提示', '确定删除吗？', function () {
            $.post(href, function (data) {
                if (data.success) {
                    window.location.reload(true);
                } else {
                    $.ActionResponse('error', '提示', "错误信息：" + data.data);
                }
            }, 'json');

        });
        return false;
    });

    var href = $($(".search")[1]).attr("href");
    $(".datalist").load(href, function (data) {
        $("#pageNum").paginate({
            count: $("#tbdatalist").attr("pagecount"),
            start: $("#tbdatalist").attr("pageindex"),
            display: 10,
            border: false,
            text_color: '#888',
            background_color: '#EEE',
            text_hover_color: 'black',
            background_hover_color: '#CFCFCF',
            onChange: function (page) {
                chageurl = href.replace(href.substring(href.indexOf('=') + 1, href.indexOf('&')), page);
                LoadPage(chageurl);
            }
        });
    });

    function LoadPage(href) {
        $(".datalist").load(href, function (data) {
            $("#pageNum").paginate({
                count: $("#tbdatalist").attr("pagecount"),
                start: $("#tbdatalist").attr("pageindex"),
                display: 10,
                border: false,
                text_color: '#888',
                background_color: '#EEE',
                text_hover_color: 'black',
                background_hover_color: '#CFCFCF',
                onChange: function (page) {
                    chageurl = href.replace(href.substring(href.indexOf('=') + 1, href.indexOf('&')), page);
                    LoadPage(chageurl);
                }
            });
        });
    }



};
